﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;
using System.Data.SqlClient;

using SIPO.Library;
using SIPO.Library.DomainObject.Administration.Catalog;
using SIPO.Library.Enum;
using SIPO.Library.Interfaces;

namespace SIPO.Library.DataAccess.Administration.Catalog
{

    public partial class recSubSubSectorRepo : ISearchable<recSubSubSector>
    {

        public object SelectScalar(SQL.Function.Aggregate function, string column, string parameters)
        {
            object _result = null;
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    StringBuilder sbQuery = new StringBuilder();
                    switch (function)
                    {
                        case SQL.Function.Aggregate.Max:
                            sbQuery.AppendFormat("SELECT MAX({0}) FROM master.recSubSubSector {1}", column, parameters);
                            break;
                        case SQL.Function.Aggregate.Min:
                            sbQuery.AppendFormat("SELECT MIN({0}) FROM master.recSubSubSector {1}", column, parameters);
                            break;
                        case SQL.Function.Aggregate.Distinct:
                            sbQuery.AppendFormat("SELECT DISTINCT({0}) FROM master.recSubSubSector {1}", column, parameters);
                            break;
                        case SQL.Function.Aggregate.Count:
                            sbQuery.AppendFormat("SELECT COUNT(*) FROM master.recSubSubSector ");
                            sbQuery.AppendFormat("      INNER JOIN master.recSubSector ON (recSubSubSector.SubSectorID = recSubSector.SubSectorID) ");
                            sbQuery.AppendFormat("      INNER JOIN master.recSector ON (recSubSector.SectorID = recSector.SectorID) ");
                            sbQuery.AppendFormat("      LEFT JOIN master.recRegistrationLabel ON (recSubSubSector.RegistrationLabelID = recRegistrationLabel.RegistrationLabelID) ");
                            if (parameters.Contains("debtorid"))
                                sbQuery.AppendFormat("      LEFT JOIN master.recDebtorSector ON (recDebtorSector.SubSubSectorID =  recSubSubSector.SubSubSectorID ) ");
                            //sbQuery.AppendFormat("      LEFT JOIN master.recDebtorSector ON (recDebtorSector.SubSubSectorID =  recSubSubSector.SubSubSectorID ) ");
                            sbQuery.AppendFormat("    {0}", parameters);
                            break;
                        case SQL.Function.Aggregate.Sum:
                            sbQuery.AppendFormat("SELECT SUM({0}) FROM master.recSubSubSector {1}", column, parameters);
                            break;
                        case SQL.Function.Aggregate.Avg:
                            sbQuery.AppendFormat("SELECT AVG({0}) FROM master.recSubSubSector {1}", column, parameters);
                            break;
                        default:
                            // do nothing 
                            break;
                    }

                    cmd.CommandText = sbQuery.ToString();
                    cmd.CommandType = CommandType.Text;
                    try
                    {
                        conn.Open();
                        _result = cmd.ExecuteScalar();
                    }
                    catch (Exception ex)
                    {
                        throw (ex);
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open) conn.Close();
                    }
                }
            }
            return _result;
        }

        public List<recSubSubSector> GetList(int pageSize, int currentPage, string sortName, string sortOrder, string parameters)
        {
            List<recSubSubSector> tt = new List<recSubSubSector>();
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    int startRow = ((currentPage - 1) * pageSize) + 1;
                    int endRow = (startRow + pageSize);

                    StringBuilder sbQuery = new StringBuilder();
                    sbQuery.AppendFormat(" WITH result_set AS ");
                    sbQuery.AppendFormat(" ( ");
                    sbQuery.AppendFormat("    SELECT  ");
                    sbQuery.AppendFormat("      ROW_NUMBER() OVER (ORDER BY {0} {1}) AS [row_number], ", sortName, sortOrder);
                    sbQuery.AppendFormat(@"  recSubSubSector.SubSubSectorID
	                                            , recSubSector.SectorID
	                                            , recSector.SectorCode
	                                            , recSector.SectorName
	                                            , recSubSubSector.SubSectorID
	                                            , recSubSector.SubSectorName
	                                            , recSubSubSector.SubSubSectorCode
	                                            , recSubSubSector.SubSubSectorName
	                                            , recSubSubSector.IsIndividual
	                                            , recSubSubSector.CreatedDate
	                                            , recSubSubSector.CreatedBy
	                                            , recSubSubSector.UpdatedDate
	                                            , recSubSubSector.UpdatedBy
                                                , recRegistrationLabel.RegistrationLabelName
                                                , recSubSubSector.IsEmiten");
                    sbQuery.AppendFormat("    FROM ");
                    sbQuery.AppendFormat("      master.recSubSubSector ");
                    sbQuery.AppendFormat("      INNER JOIN master.recSubSector ON (recSubSubSector.SubSectorID = recSubSector.SubSectorID) ");
                    sbQuery.AppendFormat("      INNER JOIN master.recSector ON (recSubSector.SectorID = recSector.SectorID) ");
                    sbQuery.AppendFormat("      LEFT JOIN master.recRegistrationLabel ON (recSubSubSector.RegistrationLabelID = recRegistrationLabel.RegistrationLabelID) ");
                    if (parameters.Contains("debtorid"))
                        sbQuery.AppendFormat("      LEFT JOIN master.recDebtorSector ON (recDebtorSector.SubSubSectorID =  recSubSubSector.SubSubSectorID ) ");
                    sbQuery.AppendFormat("    {0}", parameters);
                    sbQuery.AppendFormat(" ) ");
                    sbQuery.AppendFormat(" SELECT * FROM result_set WHERE [row_number] BETWEEN {0} AND {1} ", startRow, endRow);

                    cmd.CommandText = sbQuery.ToString();
                    cmd.CommandType = CommandType.Text;
                    try
                    {
                        conn.Open();
                        using (SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                        {
                            while (dr.Read())
                            {
                                recSubSubSector t = new recSubSubSector(true);
                                t.SubSubSectorID = dr[recSubSubSector.Fields.SubSubSectorID] == DBNull.Value ? 0 : int.Parse(dr[recSubSubSector.Fields.SubSubSectorID].ToString());
                                t.SubSectorID = dr[recSubSubSector.Fields.SubSectorID] == DBNull.Value ? 0 : int.Parse(dr[recSubSubSector.Fields.SubSectorID].ToString());
                                t.SubSubSectorCode = dr[recSubSubSector.Fields.SubSubSectorCode] == DBNull.Value ? string.Empty : dr[recSubSubSector.Fields.SubSubSectorCode].ToString();
                                t.SubSubSectorName = dr[recSubSubSector.Fields.SubSubSectorName] == DBNull.Value ? string.Empty : dr[recSubSubSector.Fields.SubSubSectorName].ToString();
                                t.IsIndividual = dr[recSubSubSector.Fields.IsIndividual] == DBNull.Value ? false : bool.Parse(dr[recSubSubSector.Fields.IsIndividual].ToString());
                                t.IsEmiten = dr[recSubSubSector.Fields.IsEmiten] == DBNull.Value ? false : bool.Parse(dr[recSubSubSector.Fields.IsEmiten].ToString());

                                t.recSubSector.SubSectorName = dr[recSubSector.Fields.SubSectorName] == DBNull.Value ? string.Empty : dr[recSubSector.Fields.SubSectorName].ToString();
                                t.recSubSector.recSector.SectorName = dr[recSector.Fields.SectorName] == DBNull.Value ? string.Empty : dr[recSector.Fields.SectorName].ToString();

                                t.CreatedDate = dr[recSubSubSector.Fields.CreatedDate] == DBNull.Value ? (DateTime?)null : DateTime.Parse(dr[recSubSubSector.Fields.CreatedDate].ToString());
                                t.CreatedBy = dr[recSubSubSector.Fields.CreatedBy] == DBNull.Value ? string.Empty : dr[recSubSubSector.Fields.CreatedBy].ToString();
                                t.UpdatedDate = dr[recSubSubSector.Fields.UpdatedDate] == DBNull.Value ? (DateTime?)null : DateTime.Parse(dr[recSubSubSector.Fields.UpdatedDate].ToString());
                                t.UpdatedBy = dr[recSubSubSector.Fields.UpdatedBy] == DBNull.Value ? string.Empty : dr[recSubSubSector.Fields.UpdatedBy].ToString();

                                t.recRegistrationLabel.RegistrationLabelName = dr[recRegistrationLabel.Fields.RegistrationLabelName] == DBNull.Value ? string.Empty : dr[recRegistrationLabel.Fields.RegistrationLabelName].ToString();

                                tt.Add(t);
                                t.Dispose();
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        throw (ex);
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open) conn.Close();
                    }
                }
            }
            return tt;
        }

        public List<recSubSubSector> GetListById(int key)
        {
            List<recSubSubSector> tt = new List<recSubSubSector>();
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "SELECT SubSubSectorID, SubSectorID, SubSubSectorCode, SubSubSectorName, IsIndividual, RegistrationLabelID, CreatedDate, CreatedBy, UpdatedDate, UpdatedBy FROM master.recSubSubSector WHERE SubSubSectorID = @SubSubSectorID";
                    cmd.CommandType = CommandType.Text;
                    cmd.Parameters.Add("@SubSubSectorID", SqlDbType.Int).Value = key;
                    try
                    {
                        conn.Open();
                        using (SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                        {
                            while (dr.Read())
                            {
                                recSubSubSector t = new recSubSubSector();
                                t.SubSubSectorID = dr[recSubSubSector.Fields.SubSubSectorID] == DBNull.Value ? 0 : int.Parse(dr[recSubSubSector.Fields.SubSubSectorID].ToString());
                                t.SubSectorID = dr[recSubSubSector.Fields.SubSectorID] == DBNull.Value ? 0 : int.Parse(dr[recSubSubSector.Fields.SubSectorID].ToString());
                                t.SubSubSectorCode = dr[recSubSubSector.Fields.SubSubSectorCode] == DBNull.Value ? string.Empty : dr[recSubSubSector.Fields.SubSubSectorCode].ToString();
                                t.SubSubSectorName = dr[recSubSubSector.Fields.SubSubSectorName] == DBNull.Value ? string.Empty : dr[recSubSubSector.Fields.SubSubSectorName].ToString();
                                t.IsIndividual = dr[recSubSubSector.Fields.IsIndividual] == DBNull.Value ? false : bool.Parse(dr[recSubSubSector.Fields.IsIndividual].ToString());
                                t.RegistrationLabelID = dr[recSubSubSector.Fields.RegistrationLabelID] == DBNull.Value ? (int?)null : int.Parse(dr[recSubSubSector.Fields.RegistrationLabelID].ToString());
                                t.CreatedDate = dr[recSubSubSector.Fields.CreatedDate] == DBNull.Value ? (DateTime?)null : DateTime.Parse(dr[recSubSubSector.Fields.CreatedDate].ToString());
                                t.CreatedBy = dr[recSubSubSector.Fields.CreatedBy] == DBNull.Value ? string.Empty : dr[recSubSubSector.Fields.CreatedBy].ToString();
                                t.UpdatedDate = dr[recSubSubSector.Fields.UpdatedDate] == DBNull.Value ? (DateTime?)null : DateTime.Parse(dr[recSubSubSector.Fields.UpdatedDate].ToString());
                                t.UpdatedBy = dr[recSubSubSector.Fields.UpdatedBy] == DBNull.Value ? string.Empty : dr[recSubSubSector.Fields.UpdatedBy].ToString();

                                tt.Add(t);
                                t.Dispose();
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        throw (ex);
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open) conn.Close();
                    }
                }
            }
            return tt;
        }

        public bool CheckDataByCode(string code, int subsectorID)
        {
            bool _result = false;
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    StringBuilder sbQuery = new StringBuilder();

                    sbQuery.AppendFormat("SELECT COUNT(SubSubSectorCode) SubSubSectorCode FROM master.recSubSubSector WHERE SubSubSectorCode=@SubSubSectorCode AND SubSectorID=@SubSectorID");
                    cmd.Parameters.Add("@SubSubSectorCode", SqlDbType.VarChar).Value = code;
                    cmd.Parameters.Add("@SubSectorID", SqlDbType.SmallInt).Value = subsectorID;

                    cmd.CommandText = sbQuery.ToString();
                    cmd.CommandType = CommandType.Text;

                    try
                    {
                        conn.Open();

                        var objId = cmd.ExecuteScalar();
                        objId = objId == DBNull.Value ? 0 : int.Parse(objId.ToString());

                        _result = int.Parse(objId.ToString()) == 0 ? false : true;

                    }
                    catch (Exception ex)
                    {
                        throw (ex);
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open) conn.Close();
                    }
                }
            }
            return _result;
        }

        public List<recSubSubSector> GetListMultipleSubSectorID(string parameter)
        {
            List<recSubSubSector> tt = new List<recSubSubSector>();
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    StringBuilder sbQuery = new StringBuilder();

                    sbQuery.AppendFormat(@"SELECT recSector.SectorID,recSubSector.SubSectorID, recSector.SectorCode,recSubSector.SubSectorCode ,
                                            recSubSubSector.SubSubSectorID, recSubSubSector.SubSubSectorCode,
                                            recSector.SectorName,recSubSector.SubSectorName
                                            ,recSubSubSector.SubSubSectorName
                                            FROM
                                            master.recSector
                                            inner join master.recSubSector on recSubSector.SectorID = recSector.SectorID
                                            inner join master.recSubSubSector on recSubSubSector.SubSectorID = recSubSector.SubSectorID {0}", parameter);
                    cmd.CommandText = sbQuery.ToString();
                    cmd.CommandType = CommandType.Text;
                    try
                    {
                        conn.Open();
                        using (SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                        {
                            while (dr.Read())
                            {
                                recSubSubSector t = new recSubSubSector(true);
                                t.recSubSector.SectorID = dr[recSector.Fields.SectorID] == DBNull.Value ? 0 : int.Parse(dr[recSector.Fields.SectorID].ToString());
                                t.SubSectorID = dr[recSubSector.Fields.SubSectorID] == DBNull.Value ? 0 : int.Parse(dr[recSubSector.Fields.SubSectorID].ToString());
                                t.SubSubSectorID = dr[recSubSubSector.Fields.SubSubSectorID] == DBNull.Value ? 0 : int.Parse(dr[recSubSubSector.Fields.SubSubSectorID].ToString());
                                t.SubSubSectorCode = dr[recSubSubSector.Fields.SubSubSectorCode] == DBNull.Value ? string.Empty : dr[recSubSubSector.Fields.SubSubSectorCode].ToString();
                                t.SubSubSectorName = dr[recSubSubSector.Fields.SubSubSectorName] == DBNull.Value ? string.Empty : dr[recSubSubSector.Fields.SubSubSectorName].ToString();
                                t.recSubSector.SubSectorCode = dr[recSubSector.Fields.SubSectorCode] == DBNull.Value ? string.Empty : dr[recSubSector.Fields.SubSectorCode].ToString();
                                t.recSubSector.recSector.SectorCode = dr[recSector.Fields.SectorCode] == DBNull.Value ? string.Empty : dr[recSector.Fields.SectorCode].ToString();
                                tt.Add(t);
                                t.Dispose();
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        throw (ex);
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open) conn.Close();
                    }
                }
            }
            return tt;
        }

        public List<recSubSubSector> GetListBySubSectorID_AND_SubSubSectorIDs(int subsectorID, string subsubsectorIDs)
        {
            List<recSubSubSector> tt = new List<recSubSubSector>();
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    StringBuilder sbQuery = new StringBuilder();

                    sbQuery.AppendFormat(@"SELECT SubSubSectorID
                                            , SubSectorID
                                            , SubSubSectorCode
                                            , SubSubSectorName
                                            , IsEmiten
                                            , IsIndividual
                                            , RegistrationLabelID
                                            , CreatedDate
                                            , CreatedBy
                                            , UpdatedDate
                                            , UpdatedBy
                                            FROM master.recSubSubSector WHERE SubSectorID={0} AND SubSubSectorID IN ({1})", subsectorID, subsubsectorIDs);
                    cmd.CommandText = sbQuery.ToString();
                    cmd.CommandType = CommandType.Text;
                    try
                    {
                        conn.Open();
                        using (SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                        {
                            while (dr.Read())
                            {
                                recSubSubSector t = new recSubSubSector();
                                t.SubSubSectorID = dr[recSubSubSector.Fields.SubSubSectorID] == DBNull.Value ? 0 : int.Parse(dr[recSubSubSector.Fields.SubSubSectorID].ToString());
                                t.SubSectorID = dr[recSubSubSector.Fields.SubSectorID] == DBNull.Value ? 0 : int.Parse(dr[recSubSubSector.Fields.SubSectorID].ToString());
                                t.SubSubSectorCode = dr[recSubSubSector.Fields.SubSubSectorCode] == DBNull.Value ? string.Empty : dr[recSubSubSector.Fields.SubSubSectorCode].ToString();
                                t.SubSubSectorName = dr[recSubSubSector.Fields.SubSubSectorName] == DBNull.Value ? string.Empty : dr[recSubSubSector.Fields.SubSubSectorName].ToString();
                                t.IsEmiten = dr[recSubSubSector.Fields.IsEmiten] == DBNull.Value ? false : bool.Parse(dr[recSubSubSector.Fields.IsEmiten].ToString());
                                t.IsIndividual = dr[recSubSubSector.Fields.IsIndividual] == DBNull.Value ? false : bool.Parse(dr[recSubSubSector.Fields.IsIndividual].ToString());
                                t.RegistrationLabelID = dr[recSubSubSector.Fields.RegistrationLabelID] == DBNull.Value ? (int?)null : int.Parse(dr[recSubSubSector.Fields.RegistrationLabelID].ToString());
                                t.CreatedDate = dr[recSubSubSector.Fields.CreatedDate] == DBNull.Value ? (DateTime?)null : DateTime.Parse(dr[recSubSubSector.Fields.CreatedDate].ToString());
                                t.CreatedBy = dr[recSubSubSector.Fields.CreatedBy] == DBNull.Value ? string.Empty : dr[recSubSubSector.Fields.CreatedBy].ToString();
                                t.UpdatedDate = dr[recSubSubSector.Fields.UpdatedDate] == DBNull.Value ? (DateTime?)null : DateTime.Parse(dr[recSubSubSector.Fields.UpdatedDate].ToString());
                                t.UpdatedBy = dr[recSubSubSector.Fields.UpdatedBy] == DBNull.Value ? string.Empty : dr[recSubSubSector.Fields.UpdatedBy].ToString();
                                tt.Add(t);
                                t.Dispose();
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        throw (ex);
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open) conn.Close();
                    }
                }
            }
            return tt;
        }

        public List<recSubSubSector> GetListByDebtorID_AND_SubSubSectorIDs(int debtorID, string subsubsectorIDs)
        {
            List<recSubSubSector> tt = new List<recSubSubSector>();
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    StringBuilder sbQuery = new StringBuilder();

                    sbQuery.AppendFormat(@"select master.recSubSubSector.SubSubSectorID,master.recSubSubSector.SubSubSectorCode,master.recSubSubSector.SubSubSectorName from master.recDebtor
                                            inner join master.recDebtorSector on master.recDebtorSector.DebtorID=master.recDebtor.DebtorID
                                            inner join master.recSubSubSector on master.recSubSubSector.SubSubSectorID=master.recDebtorSector.SubSubSectorID
                                            where master.recDebtor.DebtorID={0} and master.recSubSubSector.SubSubSectorID IN ({1})", debtorID, subsubsectorIDs);
                    cmd.CommandText = sbQuery.ToString();
                    cmd.CommandType = CommandType.Text;
                    try
                    {
                        conn.Open();
                        using (SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                        {
                            while (dr.Read())
                            {
                                recSubSubSector t = new recSubSubSector();
                                t.SubSubSectorID = dr[recSubSubSector.Fields.SubSubSectorID] == DBNull.Value ? 0 : int.Parse(dr[recSubSubSector.Fields.SubSubSectorID].ToString());
                                t.SubSubSectorCode = dr[recSubSubSector.Fields.SubSubSectorCode] == DBNull.Value ? string.Empty : dr[recSubSubSector.Fields.SubSubSectorCode].ToString();
                                t.SubSubSectorName = dr[recSubSubSector.Fields.SubSubSectorName] == DBNull.Value ? string.Empty : dr[recSubSubSector.Fields.SubSubSectorName].ToString();
                                tt.Add(t);
                                t.Dispose();
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        throw (ex);
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open) conn.Close();
                    }
                }
            }
            return tt;
        }

        public List<recSubSubSector> GetListByDebtorID_SubSectorID_AND_SubSubSectorIDs(int debtorID, int subSectorID, string subsubsectorIDs)
        {
            List<recSubSubSector> tt = new List<recSubSubSector>();
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    StringBuilder sbQuery = new StringBuilder();

                    sbQuery.AppendFormat(@"select master.recSubSubSector.SubSubSectorID,master.recSubSubSector.SubSubSectorCode,master.recSubSubSector.SubSubSectorName from master.recDebtor
                                            inner join master.recDebtorSector on master.recDebtorSector.DebtorID=master.recDebtor.DebtorID
                                            inner join master.recSubSubSector on master.recSubSubSector.SubSubSectorID=master.recDebtorSector.SubSubSectorID
                                            where master.recDebtor.DebtorID={0} and master.recDebtorSector.SubSectorID ={1} and master.recSubSubSector.SubSubSectorID IN ({2})", debtorID, subSectorID, subsubsectorIDs);
                    cmd.CommandText = sbQuery.ToString();
                    cmd.CommandType = CommandType.Text;
                    try
                    {
                        conn.Open();
                        using (SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                        {
                            while (dr.Read())
                            {
                                recSubSubSector t = new recSubSubSector();
                                t.SubSubSectorID = dr[recSubSubSector.Fields.SubSubSectorID] == DBNull.Value ? 0 : int.Parse(dr[recSubSubSector.Fields.SubSubSectorID].ToString());
                                t.SubSubSectorCode = dr[recSubSubSector.Fields.SubSubSectorCode] == DBNull.Value ? string.Empty : dr[recSubSubSector.Fields.SubSubSectorCode].ToString();
                                t.SubSubSectorName = dr[recSubSubSector.Fields.SubSubSectorName] == DBNull.Value ? string.Empty : dr[recSubSubSector.Fields.SubSubSectorName].ToString();
                                tt.Add(t);
                                t.Dispose();
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        throw (ex);
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open) conn.Close();
                    }
                }
            }
            return tt;
        }

        public recSubSubSector GetRegistrationLabelNameById(int key)
        {
            recSubSubSector t = null;
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    StringBuilder sbQuery = new StringBuilder();

                    sbQuery.AppendFormat(@"SELECT master.recRegistrationLabel.RegistrationLabelName from master.recSubSubSector 
                                            inner join master.recRegistrationLabel 
                                            on master.recSubSubSector.RegistrationLabelID=master.recRegistrationLabel.RegistrationLabelID
                                            where master.recSubSubSector.SubSubSectorID = {0}", key);
                    cmd.CommandText = sbQuery.ToString();
                    cmd.CommandType = CommandType.Text;
                    try
                    {
                        conn.Open();
                        using (SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                        {
                            while (dr.Read())
                            {
                                t = new recSubSubSector(true);
                                t.recRegistrationLabel.RegistrationLabelName = dr[recRegistrationLabel.Fields.RegistrationLabelName] == DBNull.Value ? string.Empty : dr[recRegistrationLabel.Fields.RegistrationLabelName].ToString();
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        throw (ex);
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open) conn.Close();
                    }
                }
            }
            return t;
        }

        public recSubSubSector GetBySubSubSectorCode(int subsubsectorcode,int subsectorid)
        {
            recSubSubSector t = null;
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "SELECT SubSubSectorID, SubSectorID, SubSubSectorCode, SubSubSectorName, IsEmiten, IsIndividual, RegistrationLabelID, CreatedDate, CreatedBy, UpdatedDate, UpdatedBy FROM master.recSubSubSector WHERE SubSubSectorCode = @SubSubSectorCode and SubSectorID=@SubSectorID ";
                    cmd.CommandType = CommandType.Text;
                    cmd.Parameters.Add("@SubSubSectorCode", SqlDbType.Int).Value = subsubsectorcode;
                    cmd.Parameters.Add("@SubSectorID", SqlDbType.Int).Value = subsectorid;
                    try
                    {
                        conn.Open();
                        using (SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                        {
                            while (dr.Read())
                            {
                                t = new recSubSubSector();
                                t.SubSubSectorID = dr[recSubSubSector.Fields.SubSubSectorID] == DBNull.Value ? 0 : int.Parse(dr[recSubSubSector.Fields.SubSubSectorID].ToString());
                                t.SubSectorID = dr[recSubSubSector.Fields.SubSectorID] == DBNull.Value ? 0 : int.Parse(dr[recSubSubSector.Fields.SubSectorID].ToString());
                                t.SubSubSectorCode = dr[recSubSubSector.Fields.SubSubSectorCode] == DBNull.Value ? string.Empty : dr[recSubSubSector.Fields.SubSubSectorCode].ToString();
                                t.SubSubSectorName = dr[recSubSubSector.Fields.SubSubSectorName] == DBNull.Value ? string.Empty : dr[recSubSubSector.Fields.SubSubSectorName].ToString();
                                t.IsEmiten = dr[recSubSubSector.Fields.IsEmiten] == DBNull.Value ? false : bool.Parse(dr[recSubSubSector.Fields.IsEmiten].ToString());
                                t.IsIndividual = dr[recSubSubSector.Fields.IsIndividual] == DBNull.Value ? false : bool.Parse(dr[recSubSubSector.Fields.IsIndividual].ToString());
                                t.RegistrationLabelID = dr[recSubSubSector.Fields.RegistrationLabelID] == DBNull.Value ? (int?)null : int.Parse(dr[recSubSubSector.Fields.RegistrationLabelID].ToString());
                                t.CreatedDate = dr[recSubSubSector.Fields.CreatedDate] == DBNull.Value ? (DateTime?)null : DateTime.Parse(dr[recSubSubSector.Fields.CreatedDate].ToString());
                                t.CreatedBy = dr[recSubSubSector.Fields.CreatedBy] == DBNull.Value ? string.Empty : dr[recSubSubSector.Fields.CreatedBy].ToString();
                                t.UpdatedDate = dr[recSubSubSector.Fields.UpdatedDate] == DBNull.Value ? (DateTime?)null : DateTime.Parse(dr[recSubSubSector.Fields.UpdatedDate].ToString());
                                t.UpdatedBy = dr[recSubSubSector.Fields.UpdatedBy] == DBNull.Value ? string.Empty : dr[recSubSubSector.Fields.UpdatedBy].ToString();
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        throw (ex);
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open) conn.Close();
                    }
                }
            }
            return t;
        }

    }

}
